﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.IO.Ports;
using Microsoft.Win32;  

namespace serialPort_tool
{
    /// <summary>
    /// port_params.xaml 的交互逻辑
    /// </summary>
    public partial class port_params : Window
    {
        public string[] port_num = new string[] { "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9" };
        public string[] baud_rate = new string[] { "300", "600", "1200", "2400", "4800", "9600", "19200", "38400", "115200" };
        public string[] check_bit = new string[] { "None", "Odd", "Even", "Mark", "Space" };
        public string[] data_bit = new string[] { "8", "7", "6" };
        public string[] stop_bit = new string[] { "1", "1.5", "2" };

        public port_params()
        {
            InitializeComponent();

            com_comboBox.ItemsSource = port_num;
            rate_comboBox.ItemsSource = baud_rate;
            check_comboBox.ItemsSource = check_bit;
            data_comboBox.ItemsSource = data_bit;
            stop_comboBox.ItemsSource = stop_bit;

            try
            {
                int i = 0;
                string output;
                
                StreamReader sr = File.OpenText("..\\Config.txt");
                while ((output = sr.ReadLine()) != null)
                {
                    if (i == 0)
                       com_comboBox.SelectedIndex = Array.IndexOf(port_num, output);
                    else if (i == 1)
                        rate_comboBox.SelectedIndex = Array.IndexOf(baud_rate, output);
                    else if (i == 2)
                        check_comboBox.SelectedIndex = Array.IndexOf(check_bit, output);
                    else if (i == 3)
                        data_comboBox.SelectedIndex = Array.IndexOf(data_bit, output);
                    else if (i == 4)
                        stop_comboBox.SelectedIndex = Array.IndexOf(stop_bit, output);
                    i++;
                }
                sr.Close();
            }
            catch
            { 
                com_comboBox.SelectedIndex = 0;
                rate_comboBox.SelectedIndex = 2;
                check_comboBox.SelectedIndex = 2;
                data_comboBox.SelectedIndex = 0;
                stop_comboBox.SelectedIndex = 0;
            }
        }

        private void Ok_Button_Click(object sender, RoutedEventArgs e)
        {
            FileStream config_file = new FileStream("..\\Config.txt", FileMode.Create);
            StreamWriter config_file_w = new StreamWriter(config_file);

            config_file_w.Write(port_num[com_comboBox.SelectedIndex] + "\r\n");
            config_file_w.Write(baud_rate[rate_comboBox.SelectedIndex] + "\r\n");
            config_file_w.Write(check_bit[check_comboBox.SelectedIndex] + "\r\n");
            config_file_w.Write(data_bit[data_comboBox.SelectedIndex] + "\r\n");
            config_file_w.Write(stop_bit[stop_comboBox.SelectedIndex] + "\r\n");

            config_file_w.Close();
            config_file.Close();
    
            this.Close();
        }

        private void Cancel_Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

       private void com_comboBox_DropDownOpened(object sender, EventArgs e)
       {
           //int i;
           // RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
           // if (keyCom != null)
           // {
           //     string[] sSubKeys = keyCom.GetValueNames();
           //     foreach (string sName in sSubKeys)
           //     {
           //         string sValue = (string)keyCom.GetValue(sName);

           //         i = Array.IndexOf(port_num, sValue);
           //         if (i != -1)
           //         {
           //             port_num[i] += "(可用)";
           //         }
           //     }
           // }

           //string[] ports;

           //ports = SerialPort.GetPortNames();
           //if (ports.Length > 0)
           //{
           //    com_comboBox.ItemsSource = ports;
           //}
           //else 
           //{
           //    MessageBox.Show("没有可用的串口！"); 
           //}
        }
    }
}
